package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
    }
    @Override
    public void save(Role role, Long[] ids) {
        roleMapper.insert(role);
        if(ids!=null&&ids.length>0){
            for (Long rid : ids) {
                roleMapper.insertRelation(role.getId(),rid);
            }
        }
    }

    @Override
    public void update(Role role, Long[] ids) {
        roleMapper.updateByPrimaryKey(role);
        //删除关系
        roleMapper.deleteByRoleId(role.getId());
        //关联关系
        if(ids!=null&&ids.length>0){
            for (Long rid : ids) {
                roleMapper.insertRelation(role.getId(),rid);
            }
        }
    }


    @Override
    public Role get(Long id) {
        Role role = roleMapper.selectByPrimaryKey(id);
        return role;
    }

    @Override
    public List<Role> listAll() {
        List<Role> list = roleMapper.selectAll();
        return list;
    }

    @Override
    public PageInfo<Role> query(QueryObject qo) {
        /*if(count==0){
            return new PageResult<>(Collections.emptyList(),qo.getCurrentPage(),qo.getPageSize(),count);
        }else {
            List<Role> roles = roleMapper.selectForList(qo);
            return new PageResult<>(roles,qo.getCurrentPage(),qo.getPageSize(),count);
        }*/
         PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Role> roles = roleMapper.selectForList(qo);
        return new PageInfo<>(roles);
    }

    @Override
    public List<Permission> selectByRoleId(Long id) {
        return roleMapper.selectByRoleId(id);
    }

    @Override
    public List<Role> selectByEmpId(Long id) {
        return roleMapper.selectByEmpId(id);
    }
}
